package com.example.asus.yuejian_10.adapter;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.example.asus.yuejian_10.Config;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.bean.VideoList;

import java.util.List;


/**
 * Created by Administrator on 2017/10/2 0002.
 */

public class VideoAdapter  extends RecyclerView.Adapter{
    private static  int UpUI=1;
    private VideoViewHolder videoViewHolder;
    private Context context;
    List<VideoList.MvideoEntity> list;
    public VideoAdapter(Context context,List<VideoList.MvideoEntity> list)
    {
        this.context=context;
        this.list=list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_of_video,viewGroup,false);
        VideoViewHolder videoViewHolder=new VideoViewHolder(view);
        return videoViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        VideoList.MvideoEntity  entity=list.get(i);
        videoViewHolder= (VideoViewHolder) viewHolder;
        videoViewHolder.tiele.setText(entity.getMvideo_name());
        videoViewHolder.hint.setText(entity.getHits());
        //视频地址
        controllerVideo();
        Uri videoURL= Uri.parse(Config.VIDEO_URL+entity.getUrl());
        /*MediaController controller=new MediaController(context);
        videoViewHolder.videoView.setMediaController(controller);
        controller.setMediaPlayer(videoViewHolder.videoView);*/
        videoViewHolder.videoView.setVideoPath(videoURL.toString());
        videoViewHolder.videoView.start();
        UIhandler.sendEmptyMessage(UpUI);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class VideoViewHolder extends RecyclerView.ViewHolder{
        VideoView videoView;
        TextView  tiele;
        ImageView video_play,video_expand;
        TextView hint,current_time,total_time;
        SeekBar programe;

        public VideoViewHolder(View itemView) {
            super(itemView);
            programe=(SeekBar)itemView.findViewById(R.id.programe);
            video_play=(ImageView)itemView.findViewById(R.id.video_play);
            video_expand=(ImageView)itemView.findViewById(R.id.video_expand);
            videoView=(VideoView)itemView.findViewById(R.id.tea_detail_video);
            tiele=(TextView)itemView.findViewById(R.id.tea_video_title);
            hint=(TextView)itemView.findViewById(R.id.tea_video_hint);
            current_time=(TextView)itemView.findViewById(R.id.current_time);
            total_time=(TextView)itemView.findViewById(R.id.total_time);
        }
    }
    public void controllerVideo()
    {
        videoViewHolder.video_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (videoViewHolder.videoView.isPlaying())
                {
                    videoViewHolder.video_play.setImageResource(R.drawable.back);
                    videoViewHolder.videoView.pause();
                    UIhandler.removeMessages(UpUI);
                }
                else {
                    videoViewHolder.video_play.setImageResource(R.drawable.back);
                    videoViewHolder.videoView.start();
                    UIhandler.sendEmptyMessage(UpUI);
                }
            }
        });
        videoViewHolder.programe.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }
    public void timeFormat(TextView textView,int million)
    {
           int second=million/1000;
           int hh=second/3600;
           int mm=second%3600/60;
           int ss=second%60;
           String str=null;
            if (hh!=0)
            {
                str=String.format("%02d:%02d:%02d",hh,mm,ss);
            }
            else{
                str=String.format("%02d:%02d",mm,ss);
            }
            textView.setText(str);
    }
    private Handler UIhandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==UpUI){
                //得到视频当前的播放时间
                int currentPosition=videoViewHolder.videoView.getCurrentPosition();
                //得到视频当前的播放总时间
                int totalDurlation=videoViewHolder.videoView.getDuration();
                //更新当前播放时间
                timeFormat(videoViewHolder.current_time,currentPosition);
                timeFormat(videoViewHolder.total_time,totalDurlation);
                videoViewHolder.programe.setMax(totalDurlation);
                videoViewHolder.programe.setProgress(currentPosition);
                UIhandler.sendEmptyMessageDelayed(UpUI,500);
            }

        }
    };
}
